<?php


namespace app\api\controller\v1;


use app\api\model\NPAModel;
use app\common\controller\UserBaseController;

class NpareaController extends UserBaseController {

    function infoAction() {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new NPAModel())->info($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function listAction() {
        $this->paramlist = array(
            'token' => 'string',
            'state' => 'optint',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'flush' => ['optint', 0],
            'city_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        $list = (new NPAModel())->get_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        if (empty($list)) {
            $this->resp['total'] = 0;
        } else {
            $this->resp['total'] = hisql::query_count();
        }

        return $this->ret_resp();
    }

    function addAction() {
        $this->paramlist = array(
            'token' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'coordinates' => 'polygon',
            'state' => 'int',
            'type' => 'string',
            'is_no_park' => ['optint',0]
        );

        $this->check_params();
        $this->check_token();



        $info = [
            'city_id' => $this->city_id,
            'name' => $this->argv['name'],
            'remark' => $this->argv['remark'],
            'coordinates' => $this->argv['coordinates'],
            'state' => $this->argv['state'],
            'type' => $this->argv['type'],
            'is_no_park' => 0,
            //'creator_id' => $this->argv['creator_id'],
            //  'creator_name' => $this->argv['creator_name'],
        ];
        $infox = (new NPAModel())->add($info);
        $this->resp['info'] = $infox;

        return $this->ret_resp();
    }

    function updateAction() {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'name' => 'optstring',
            'remark' => 'optstring',
            'state' => 'optint',
            'type' => 'optstring',
            'coordinates' => 'optpolygon',
            'is_no_park' => ['optint',0]
        );

        $this->check_params();
        $this->check_token();

        $info = (new NPAModel())->info($this->city_id, $this->argv['id']);
        if (empty($info)) {
            $this->throw_myexception(X_ERROR, "查询禁停区信息失败！");
        }
        $data = [];
        foreach ($this->argv as $k => $v) {
            if (isset($info[$k]) && $v !== null) {
                $data[$k] = $v;
            }
        }

        $info = (new NPAModel())->update($this->city_id, $this->argv['id'], $data);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function is_inAction() {
        $this->paramlist = array(
            'token' => 'string',
            'coordinate' => 'array',
        );
        $this->check_params();
        $this->check_token();

        $result = (new NPAModel())->is_in($this->city_id, $this->argv['coordinate']);
        $this->resp['info'] = $result;

        return $this->ret_resp();
    }

    function delAction() {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new NPAModel())->delete_npa($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

}
